import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Table, Row, Col, Spin} from 'antd';
import {TABLE} from '../constants';
import {formatAmount} from '../utils';
import {hasPermissions} from './utils';
import {PERMISSION, formatPrintExcelUrl, formatPrintPdfUrl} from './constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import styles from './style.css';
import routes from './routes';
import Ellipsis from 'ant-design-pro/lib/Ellipsis';

const basePath = fetch.basePath || '';
const url = '/#/dcs/parts/partInvoiceTrack/detailInvoice';

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
    };
    componentDidMount() {
        this.props.init();
    }
    onPrintExcel = () => {
        window.open(formatPrintExcelUrl(this.props.partOrderPIId));
    }
    onPrintPdf = () => {
        window.open(formatPrintPdfUrl(this.props.partOrderPIId));
    }
    render() {
        const detailData = this.props.detail || {};
        const {content} = detailData;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.printExcel))
            operations.push(
                <Button key="printExcel" onClick={this.onPrintExcel} size="small">
                    {formatMessage({
                        id: 'detail.btn.printExcel',
                        defaultMessage: '打印Excel'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.printPdf))
            operations.push(
                <Button key="printPdf" onClick={this.onPrintPdf} size="small">
                    {formatMessage({
                        id: 'detail.btn.printPdf',
                        defaultMessage: '打印PDF'
                    })}
                </Button>
            );
        const columns = [{
            title: formatMessage({
                id: 'detailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.description',
                defaultMessage: '英文名称'
            }),
            dataIndex: 'description',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.price',
                defaultMessage: '单价'
            }),
            dataIndex: 'price',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.minPackSize',
                defaultMessage: '最小包装数'
            }),
            dataIndex: 'minPackSize',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.oldPartCode',
                defaultMessage: '被替换件'
            }),
            dataIndex: 'oldPartCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.approvedQuantity',
                defaultMessage: '订购数量'
            }),
            dataIndex: 'approvedQuantity',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.productCategoryCode',
                defaultMessage: '车型编号'
            }),
            dataIndex: 'productCategoryCode',
            render: text => <Ellipsis tooltip fullWidthRecognition length={32}>{text}</Ellipsis>
        }, {
            title: formatMessage({
                id: 'detailPanel.column.totalAmount',
                defaultMessage: '金额'
            }),
            dataIndex: 'totalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.quantity',
                defaultMessage: '备货数量'
            }),
            dataIndex: 'quantity',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.totalQuantity',
                defaultMessage: '出运数量'
            }),
            dataIndex: 'totalQuantity',
            render: (text, r) =>
                <a href={`${basePath}${url}?brandId=${r.brandId}&partId=${r.partId}&partSaleOrderId=${r.partSaleOrderId}`} target="_blank" rel="noopener noreferrer">{text}</a>
        }, {
            title: formatMessage({
                id: 'detailPanel.column.invoiceAmount',
                defaultMessage: '出运金额'
            }),
            dataIndex: 'invoiceAmount',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.shipmentReplacedPart',
                defaultMessage: '发货替换件'
            }),
            dataIndex: 'shipmentReplacedPart',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.categoryCode',
                defaultMessage: '维护车型'
            }),
            dataIndex: 'categoryCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.isEOL',
                defaultMessage: '是否停产'
            }),
            dataIndex: 'isEOL',
            render: text => text === true ? '是' : '否',
        }];
        return (
            <div className="detail">
                <Spin spinning={this.props.loading}>
                    <Card>
                        <DescriptionList size="large" title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'detailPanel.card.partOtherOrder',
                                defaultMessage: '备件订单状态跟踪清单'
                            })
                        }</Col>
                        
                        <Col className={`col-align-right ${styles.detail}`} span={18}>{operations}</Col></Row>}>
                            <Table
                                className="white-space-nowrap"
                                columns={columns}
                                rowKey="id"
                                pagination={false}
                                dataSource={content}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {getDetail} from './actions';
import {injectIntl} from './intl';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    permissions: getPermissions(state),
    detail: getDetailData(state),
    isDealerUser: state.getIn(['page', 'domainData', 'init', 'isDealerUser']),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
